<template>
  <div id="homecomp5">
    <div>
      <el-row>
        <el-col :span="20">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="审批人">
              <el-input
                v-model="formInline.user"
                placeholder="审批人"></el-input>
            </el-form-item>
            <el-form-item label="活动区域">
              <el-select v-model="formInline.region" placeholder="活动区域">
                <el-option label="区域一" value="shanghai"></el-option>
                <el-option label="区域二" value="beijing"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showAdd">新增</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        prop="cityId"
        label="城市ID"
        width="180"></el-table-column>
      <el-table-column
        prop="city"
        label="城市名称"
        width="180"></el-table-column>
      <el-table-column
        prop="country"
        label="国家名称"
        width="180"></el-table-column>
      <el-table-column
        prop="lastUpdate"
        label="更新时间"
        width="180"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="currentChange"
      @size-change="sizeChange"
      background
      layout=" prev, pager, next,sizes"
      :page-size="pageInfo.pageSize"
      :current-page="pageInfo.pageNum"
      :page-count="pageInfo.pages"
      :total="pageInfo.total"></el-pagination>
    <city-dialog
      :add-handler="onAdd"
      :upd-handler="onUpd"
      :form-data="form"
      :form-label-width="formLabelWidth"
      :dialog-form-visible="dialogFormVisible"
      @update:visible="dialogFormVisible = $event"
      :type="type"></city-dialog>
  </div>
</template>

<script>
import { doGet, doPost } from "@/api";
import { succ } from "@/common/msg";
import CityDialog from "@/components/dialog/city-dialog.vue";

export default {
  name: "HomeComp5",
  components: { CityDialog },
  data() {
    return {
      type: "",
      value: "",
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        cityId: 1,
        city: "",
        countryId: "",
        lastUpdate: "",
      },
      formLabelWidth: "120px",
      formInline: {
        user: "",
        region: "",
      },
      tableData: [
        {
          cityId: "1",
          city: "北京",
          country: "上海市普陀区金沙江路 1518 弄",
          lastUpdate: "2016-05-02",
        },
      ],
      pageInfo: {
        pageSize: 10,
        pageNum: 1,
        pages: 1,
        total: 1,
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    showAdd() {
      this.type = "add";
      this.dialogFormVisible = true;
    },
    onUpd() {},
    onAdd() {
      console.log("-------------------");

      doPost("city", this.form).then((res) => {
        this.dialogFormVisible = false;
        succ("添加成功！", this);
      });
    },

    getData() {
      doGet("city", this.pageInfo).then((res) => {
        this.tableData = res.list;
        this.pageInfo.pages = res.pages;
        this.pageInfo.total = res.total;
      });
    },
    sizeChange(a) {
      this.pageInfo.pageSize = a;
      this.getData();
    },
    currentChange(a) {
      this.pageInfo.pageNum = a;
      this.getData();
    },
    onSubmit() {
      console.log("submit!");
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.type = "upd";
      this.form = row;
      this.dialogFormVisible = true;
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
  },
};
</script>

<style scoped></style>
